-----------------------------------------------------------------------------------------
-- filename : board.lua
-- description : board object
-----------------------------------------------------------------------------------------

-- load needed libraries/scripts
local tile = require "tile"
local json = require "json"
local layer = require "layer"

--local variables
local board = {}
local board_mt = {__index = board}


function board.new(xPos,yPos,tileWidth,tileHeight,col,row)
	local newBoard = {
		x = xPos,
		y = yPos,
		tileWidth = tileWidth or 32,
		tileHeight = tileHeight or 32,
		col = tonumber(col) or 7,
		row = tonumber(row) or 7,
		pions = {}
	}

	newBoard.map = {}
	for i=1, newBoard.row do
		newBoard.map[i] = {}
		for j=1, newBoard.col do
			newBoard.map[i][j] = tile.new(tile.BLANK,xPos + j*newBoard.tileWidth,yPos+i*newBoard.tileHeight)
		end
	end

	return setmetatable(newBoard, board_mt)
end

function board.new(xPos,yPos,tileWidth,tileHeight,jsonFilePath)

	local t = loadJSON(jsonFilePath)

	local newBoard = {
		x = xPos,
		y = yPos,
		tileWidth = tileWidth or 32,
		tileHeight = tileHeight or 32,
		col = t.col,
		row = t.row,
		pions = {}
	}

	local back = display.newImageRect( "assets/background.png", 320,313, true )
	back:setReferencePoint(display.TopLeftReferencePoint)
	back.x = 80-0.5
	back.y = 340+1+7-341+4
	layer.insertImage(0,back)


	local idxDir = 1
	newBoard.map = {}
	for i=1, newBoard.row do
		newBoard.map[i] = {}
		for j=1, newBoard.col do
			print("tile "..t.tilemap[(i-1)*t.col+j])
			
			if(t.tilemap[(i-1)*t.col+j] == tile.DIRECTION) then
				newBoard.map[i][j] = tile.new(t.tilemap[(i-1)*t.col+j],xPos + (j-1)*newBoard.tileWidth,yPos+(i-1)*newBoard.tileHeight, newBoard.tileWidth, newBoard.tileHeight, t.direction[idxDir])
				idxDir = idxDir + 1
			else
				newBoard.map[i][j] = tile.new(t.tilemap[(i-1)*t.col+j],xPos + (j-1)*newBoard.tileWidth,yPos+(i-1)*newBoard.tileHeight, newBoard.tileWidth, newBoard.tileHeight)
			end
		end
	end

	return setmetatable(newBoard, board_mt)
end

function loadJSON( filename )
    -- set default base dir if none specified
    local base = system.ResourceDirectory
    -- create a file path for corona i/o
    local path = system.pathForFile( filename, base )
    -- will hold contents of file
    local contents
    -- io.open opens a file at path. returns nil if no file found

    local file = io.open( path, "r" )
    if file then
        -- read all contents of file into a string
        contents = file:read( "*a" )
        io.close( file )    -- close the file after using it
        --return decoded json string
        return json.decode( contents )
    else
        --or return nil if file didn't ex
        return nil
    end
end

function board:getTileType(rowPos,colPos)
	return self.map[rowPos][colPos].tile_type
end

function board:getTile(rowPos,colPos)
	return self.map[rowPos][colPos]
end

function board:addPion(_pion,row,col)
	table.insert(self.pions,_pion)
end

function board:removePion(_pion)
	local idx = -1
	for i=1,table.getn(self.pions) do
		if(self.pions[i] == _pion) then
			idx = i
			break
		end
	end
	if(idx >= 0) then
		table.remove(self.pions,idx)
	end
end

function board:getPion(row,col)
	print(self.pions.numChildren)
	for i=1,table.getn(self.pions) do
		if(self.pions[i].row == row and self.pions[i].col == col) then
			return self.pions[i]
		end
	end
	return nil
end

return board